package com.shop.web;

import com.shop.entity.*;
import com.shop.service.IAddressService;
import com.shop.service.ICartService;
import com.shop.service.IOrderService;
import com.shop.service.IProductsService;
import com.shop.service.impl.AddressServiceImpl;
import com.shop.service.impl.CartServiceImpl;
import com.shop.service.impl.OrderServiceImpl;
import com.shop.service.impl.ProductsServiceImpl;
import com.shop.util.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/order")
public class OrderServlet extends BaseServlet {

    private ICartService cartService = new CartServiceImpl();
    private IAddressService addressService = new AddressServiceImpl();
    private IOrderService orderService = new OrderServiceImpl();
    private IProductsService productsService=new ProductsServiceImpl();
    /**
     * 展示当前购物车当中所有的商品以及该用户所有的收货地址，请求转发到订单页面展示
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String preview(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User)req.getSession().getAttribute("user");
        List<Cart> carts = cartService.findCartsByUid(user.getUid());
        List<Address> address = addressService.findAddressByUid(user.getUid());
        req.setAttribute("carts",carts);
        req.setAttribute("address",address);
        return "forward:/pages/order/order.jsp";
    }

    /**
     * 生成订单
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String create(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid = ((User)req.getSession().getAttribute("user")).getUid();
        int aid = WebUtils.parseInt(req.getParameter("aid"), 0);
        orderService.create(uid, aid);
        List<Orders> orders = orderService.findAllOrderByUid(uid);
        req.getSession().setAttribute("orders",orders);
        return "redirect:/pages/order/orderList.jsp";
    }

    /**
     * 查看所有订单
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid = ((User)req.getSession().getAttribute("user")).getUid();
        List<Orders> orders = orderService.findAllOrderByUid(uid);
        req.getSession().setAttribute("orders",orders);
        return "redirect:/pages/order/orderList.jsp";
    }

    /**
     * 查看订单详情
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        Orders order = orderService.findAllOrderByOid(oid);
        req.setAttribute("order",order);
        return "forward:/pages/order/orderDetail.jsp";
    }

    /**
     * 改变订单状态
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String changeStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        int status = WebUtils.parseInt(req.getParameter("status"), 0);
        orderService.changeStatus(oid,status);
        return "redirect:/order?method=show";
    }

    /**
     * 直接购买所调用的方法
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String directBuy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session =req.getSession();
        User user =(User) session.getAttribute("user");
        List<Address> address = addressService.findAddressByUid(user.getUid());
        int pid =WebUtils.parseInt(req.getParameter("pid"),0);
        Product product = productsService.findProduct(pid);
        Cart cart = new Cart();
        cart.setCnum(1);
        cart.setCcount(product.getPprice());
        cart.setProduct(product);
        System.out.println("cart"+cart);
        req.setAttribute("cart",cart);
        req.setAttribute("address",address);
        return "forward:/pages/order/order2.jsp";

    }
}
